package com.tanhua.api.mongdb;

import com.tanhua.domain.mogo.Comment;
import com.tanhua.domain.mogo.Publish;
import com.tanhua.domain.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    //通用点赞/评论/喜欢
    public Integer save(Comment comment) {

        //1 保存comment到点赞喜欢评论记录表  quanzi_comment
                //设定固定comment的属性
        comment.setCreated(System.currentTimeMillis());  //当前系统时间
                //根据动态id获取到publish 拿到userId发布人的属性值
        Publish publish = mongoTemplate.findById(comment.getPublishId(), Publish.class);
        comment.setPublishUserId(publish.getUserId()); //发布人
        mongoTemplate.insert(comment);

        //2 对点赞喜欢评论数量+1   quanzi_publish
        Query query = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),1); //点赞、评论、喜欢
        mongoTemplate.upsert(query,update,Publish.class);

        //3 查询数量进行返回  quanzi_comment
        Query cquery = new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("commentType").is(comment.getCommentType()).and("pubType").is(comment.getPubType()));
        Long count = mongoTemplate.count(cquery, Comment.class);

        return count.intValue();
    }

    @Override
    public Integer delete(Comment comment) {

        //删除点赞 喜欢 评论记录表的一条记录数据
        Query query = new Query(Criteria.where("publishId").is(comment.getPublishId())
        .and("commentType").is(comment.getCommentType()).and("pubType").is(comment.getPubType())
        .and("userId").is(comment.getUserId()));
        mongoTemplate.remove(query,Comment.class);

        //2 对点赞喜欢评论数量+1   quanzi_publish
        Query query2 = new Query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        update.inc(comment.getCol(),-1); //点赞、评论、喜欢
        mongoTemplate.upsert(query2,update,Publish.class);

        //3 查询数量进行返回  quanzi_comment
        Query cquery = new Query(Criteria.where("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType()).and("pubType").is(comment.getPubType()));
        Long count = mongoTemplate.count(cquery, Comment.class);

        return count.intValue();
    }

    @Override
    public PageBeanVo findComments(String movementId, Integer page, Integer pagesize) {

        Query query = new Query(Criteria.where("publishId").is(new ObjectId(movementId))
        .and("pubType").is(1).and("commentType").is(2));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Comment> list = mongoTemplate.find(query, Comment.class);
        long count = mongoTemplate.count(query, Comment.class);
        PageBeanVo pageBeanVo = new PageBeanVo(page,pagesize,count,list);
        return pageBeanVo;
    }

    @Override
    public PageBeanVo findCommentPage(Integer page, Integer pagesize, int commentType, Long userId) {

        Query query = new Query(Criteria.where("commentType").is(commentType)
        .and("publishUserId").is(userId));
        query.with(new Sort(Sort.Direction.DESC,"created"));
        query.skip((page-1)*pagesize).limit(pagesize);
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);
        Long count = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(page,pagesize,count,commentList);
    }
}
